package com.qdsg.modulesihealsdk.detectStrategy.strategy.tStrategy;

import android.content.Context;

import com.example.moduleutils.LogUtil;
import com.google.gson.Gson;
import com.qdsg.modulesihealsdk.detectStrategy.control.StrategyCallback;
import com.qdsg.modulesihealsdk.entity.TResultEntity;
import com.qdsg.modulesihealsdk.utils.SihealSdkConstant;
import com.qdsg.modulesihealsdk.detectStrategy.control.DetectBusiness;
import com.siheal.device.reader.measure.TemperatureReader;
import com.siheal.device.reader.singlechip.DataChannelReader;
import com.siheal.entitys.device.DeviceOp;
import com.siheal.entitys.measure.TemperatureEntity;
import com.siheal.exception.BasicException;

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/11/21 11:36
 */
public class TStrategy implements DetectBusiness {
    private DataChannelReader dataChannelReader;
    private TemperatureReader temperatureReader;
    private Context context;
    private TCallback tCallback;
    /**
     *
     * haveGetReceive：是否已出值标志，防止出值后还去调用process方法
     */
    private Boolean haveGetReceive = false;
    @Override
    public void dealDetect(Context context, StrategyCallback strategyCallback) {
        this.context = context;
        haveGetReceive = false;
        if (strategyCallback instanceof TCallback){
            tCallback = (TCallback)strategyCallback;
            detectTemperature();
        }
    }

    @Override
    public void stopThread() {
        try {
            if (temperatureReader != null){
                temperatureReader.stopRead();
                temperatureReader = null;
            }

        }catch (Exception e){

        }
        haveGetReceive = false;
    }

    private void detectTemperature(){
        dataChannelReader = (new DataChannelReader(SihealSdkConstant.channelID_T, context){
            // 数据通道切换成功，开始调用体温读值方法
            @Override
            public void resultReceived(DeviceOp result) {
                temperatureReader = (new TemperatureReader(5000, context) {
                    //体温数据获取成功，开始做后续数据处理，当前操作将结果数据回显至H5页面
                    //获取一次结果(最终结果)，在此方法中接收检测结果
                    @Override
                    public void resultReceived(TemperatureEntity temperatureEntity) {
                        final String data = new Gson().toJson(temperatureEntity);
                        LogUtil.logAndWrite("__t-resultReceived",data);
                        //得到数据
                        //测试的数据格式：{"assess":"NORMAL","assessTemperature":"NORMAL","mode":-1,"temperature":36.8,"unit":"℃","c":"36ed05d60dc340a98c7caa99627fb709","d":0,"e":0,"gen_plan":true,"measureAssess":"正常","a":1668996132932,"b":0}
                        if (tCallback != null && !haveGetReceive){
                            haveGetReceive = true;
                            tCallback.resultReceived(new TResultEntity(temperatureEntity));
                        }

                    }
                    //持续获取检测结果，针对的是持续出值的检测设备，例如血氧设备，检测过程中设备按一定的频率返回检测结果
                    //后续处理可在一定的时间范围内选择最稳定的值作为结果显示在页面上
                    @Override
                    public void process(TemperatureEntity temperatureEntity) {
                        String data = new Gson().toJson(temperatureEntity);
                        LogUtil.logAndWrite("__t-process",data);

                        //得到数据
                        if (!haveGetReceive && tCallback != null){
                            tCallback.process(new TResultEntity(temperatureEntity));
                        }
                    }
                    //调用过程中出现的异常
                    @Override
                    public void occurError(BasicException e) {

                                   /*
                                    code = -1 测量值偏低，请将体温仪靠近额头再测量
                                    int MEASURE_OUT_RANGE_EXCEPTION = 1001;//超出范围 temperature <= 32 || temperature >= 42.9
                                    int MEASURE_ASSESS_EXCEPTION = 1002;//结果评估异常
                                    int MEASURE_TIME_OUT = 1005;//测量超时
                                   */
                        String data = new Gson().toJson(e);
                        LogUtil.logAndWrite("__t-occurError",data);
                        if (tCallback != null){
                            tCallback.occurError(e);
                        }
                    }
                });
                if (temperatureReader != null){
                    temperatureReader.start();
                }
            }
            //数据通道切换process方法，实现了父类抽象方法，在此无实义
            @Override
            public void process(DeviceOp middleResult) {}
            //数据通道切换异常处理方法
            @Override
            public void occurError(BasicException exception) {
                          /*  int ERROR_IO = 1;     //打开设备" + device.getDeviceID() + "数据通道异常
                            int ERROR_WRITE_DATA = 2;//设备" + device.getDeviceID() + "写入数据发生异常
                            int ERROR_USB_NOT_FOUND = 2001;//没有找到指定地址下(" + device.getPath() + ")设备
                            int ERROR_USB_NOT_SUPPORT = 2002;//本系统不支持USB模块
                            int ERROR_USB_UNKONW_TYPE = 2003;//未知的USB设备类型
                            int ERROR_USB_REQUEST_PERMISSION = 2004;//请求usb权限失败
                             int MEASURE_PROBE_CONFIG_ERROR = 1008;//配置文件配置错误
                            int ERROR_USB_DEVICE_OPEN = 2005;//打开usb设备错误*/
                if (tCallback != null){
                    tCallback.occurError(exception);
                }
            }
        });
        if (dataChannelReader != null){
            dataChannelReader.start();
        }

    }
}
